#Copy sample dataset
#dPath <- '/nfs/jgephart-data/Species Network/'
#file.copy(paste0(dPath, 'species_trade_2010_2011.txt'), '/research-home/ajulca/data/species_trade.txt', overwrite = FALSE)
#file.copy(paste0(dPath, 'Country_Codes_Names.txt'), '/research-home/ajulca/data/Country_Codes_Names.txt', overwrite = TRUE)

#Get shapefile
#download.file('http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip', '/research-home/ajulca/data/World_Borders.zip')
#unzip('/research-home/ajulca/data/World_Borders.zip', exdir = '/research-home/ajulca/data/')

dPath <- '/research-home/ajulca/data/'

##data.table fread not the best here; just use base read.table
#cNames <- fread(paste0(dPath, 'Country_Codes_Names.txt'), col.names = c('rNo', 'ISO_Code', 'Area_Name', 'ISO_Alpha'))
#sTrade <- fread(paste0(dPath, 'species_trade.txt'), col.names = c('rNo', 'Import', 'Export', 'Species', 'value', 'Year'), skip = 1)
cNames <- data.table(read.table(paste0(dPath, 'Country_Codes_Names.txt')))
sTrade <- data.table(read.table(paste0(dPath, 'species_trade.txt')))
setkey(cNames, key = ISO_Code)
setkey(sTrade, key = Import)

impDT <- cNames[sTrade]

setkey(impDT, key = Export)

wDT <- cNames[impDT][, .(ISO_Code, Area_Name, ISO_Alpha, Exp_Code = i.ISO_Code, Exp_Name = i.Area_Name, Exp_Alpha = i.ISO_Alpha, Species, value, Year)]

#I know Japan is a net importer of fish; let's check to make sure colnames mean what we think they mean
sum(unique(wDT[Area_Name == 'Japan', value]))
## [1] 8566.554
wDT[Year == 2010 & Area_Name == 'Japan' & Species == 'a']
##      ISO_Code Area_Name ISO_Alpha Exp_Code            Exp_Name Exp_Alpha
##   1:      392     Japan       JPN        4         Afghanistan       AFG
##   2:      392     Japan       JPN        8             Albania       ALB
##   3:      392     Japan       JPN       12             Algeria       DZA
##   4:      392     Japan       JPN       24              Angola       AGO
##   5:      392     Japan       JPN       28 Antigua and Barbuda       ATG
##  ---                                                                    
## 166:      392     Japan       JPN      854        Burkina Faso       BFA
## 167:      392     Japan       JPN      858             Uruguay       URY
## 168:      392     Japan       JPN      882               Samoa       WSM
## 169:      392     Japan       JPN      887               Yemen       YEM
## 170:      392     Japan       JPN      894              Zambia       ZMB
##      Species value Year
##   1:       a     0 2010
##   2:       a     0 2010
##   3:       a     0 2010
##   4:       a     0 2010
##   5:       a     0 2010
##  ---                   
## 166:       a     0 2010
## 167:       a     0 2010
## 168:       a     0 2010
## 169:       a     0 2010
## 170:       a     0 2010

leaflet and sp

#Based on https://rstudio.github.io/leaflet/colors.html and https://rstudio.github.io/leaflet/shiny.html and http://stackoverflow.com/questions/29118059/display-spatialpolygonsdataframe-on-leaflet-map-with-r

shapePath <- paste0(.libPaths()[1], '/fishTrade/data/worldOGR/')
rm(world)
## Warning in rm(world): object 'world' not found
tryCatch({
  world <- readOGR(shapePath, 'ne_50m_admin_0_countries', encoding='UTF-8')
  }, 
  error = function(e){
  dir.create(shapePath, showWarnings = FALSE, recursive = TRUE)
  
  download.file(file.path('http://www.naturalearthdata.com/http/',
                          'www.naturalearthdata.com/download/50m/cultural',
                          'ne_50m_admin_0_countries.zip'), 
                f <- tempfile())
  unzip(f, exdir=shapePath)
  
}, 
finally = {
  if(!exists('world')){
    world <- readOGR(shapePath, 'ne_50m_admin_0_countries', encoding='UTF-8')
  }
})
## OGR data source with driver: ESRI Shapefile 
## Source: "/research-home/ajulca/R/x86_64-pc-linux-gnu-library/3.3/fishTrade/data/worldOGR/", layer: "ne_50m_admin_0_countries"
## with 241 features
## It has 63 fields
testSP <- spTransform(world, CRS("+proj=longlat +datum=WGS84"))
mapview(testSP)
## Warning in spCheckObject(x): Columns fips_10 in attribute table contain
## only NA values and are dropped.
#maybe the approach we want to use is to reshape2 as VERY WIDE data
reshpDT <- data.table::dcast(wDT[,.(Year, Species, ISO_Alpha, Exp_Alpha, value)],  ISO_Alpha ~ Year + Species + Exp_Alpha, value.var = "value")

worldData <- merge(world, reshpDT, by.x = "iso_a3", by.y = "ISO_Alpha")

wdSP <- spTransform(worldData, CRS("+proj=longlat +datum=WGS84"))
mapview(wdSP)
## Warning in spCheckObject(x): Columns fips_10 in attribute table contain
## only NA values and are dropped.
worldDT  <- data.table(world@data)
wCoord <- coordinates(world)
colnames(wCoord) <- c('Long', 'Lat')

worldDT <- data.table(cbind(world@data, wCoord))

setkey(worldDT, key = iso_a3)
setkey(wDT, key = ISO_Alpha)

spDT <- wDT[worldDT][!is.na(Species)] 
setkey(spDT, key = Exp_Alpha)

mapDT <- spDT[worldDT][!is.na(Species), ]
setkey(mapDT, key = ISO_Alpha, Exp_Alpha)

mapDT[,
  spLine := 
    paste0('SpatialLines(list(Lines(list(Line(rbind(c(', 
      Long, ',', 
      Lat, '), c(',
      i.Long, ',', 
      i.Lat, ')))), ID = "', 
      Exp_Alpha, '")),proj4string = CRS("+init=epsg:4326"))'
    )  
]


parser <- function(thisStr){
  y <- eval(parse(text = thisStr))
  return(y)  
}

#Actually, we want our lst to change on click, with polytile erasing lines (cleaner look); for now, use example of JPN
thisISO <- 'JPN'

#spLines <- mapDT[, spLine]
spLines <- mapDT[ISO_Alpha == thisISO | Exp_Alpha == thisISO]

lst <- foreach(i = 1:length(spLines)) %do% parser(spLines[i])
#lst <- foreach(i = 1:300) %do% parser(spLines[i])

sln <- do.call("bind", lst)

mapview(sln)
#Mapview is nice, but we can also do this with leaflet - which is more compatible with shiny
leaflet() %>% addTiles() %>% 
 addPolygons(data=subset(world, iso_a3 %in% wDT[, ISO_Alpha]), weight=0.5) %>% 
# addPolygons(data=subset(world, !(iso_a3 %in% wDT[, ISO_Alpha]), weight=2))
addPolylines(data=sln, weight=1, color = "red")
#